﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using NLBS.Views;
using NLBS.Security;
using NLBS.Data.Business;

namespace NLBS.Validate
{
   public class RegisterValidate:ValidateTemplete<User>
    {
       private IUserAction userAction;
        public override void checkPower()
        {
            if (BlogInfo.Instance().EnableRegister == 0)
            {
                ShowError("注册已关闭");                
            }
            if (BlogInfo.Instance().CurrentUser.GroupID != 2)
            {
                ShowError("你已经注册");
            }
        }

        public override void checkArgument()
        {

           
            throw new NotImplementedException();
        }

        public void checkRequireField()
        {
            User user = userAction.Entity;
            string[] validfield = { user.UserName, user.Password, userAction.repassword };
            foreach (string item in validfield)
            {
                if (String.IsNullOrEmpty(item.Trim()))
                {
                    Error += "请填写所有必填栏目.";
                }

            }
        }

        public void checkwordFilterName()
        {
            Error += "用户名错误. ";
        }

        /// <summary>
        /// 检测用户是否存在
        /// </summary>
        public void checkExistName()
        {
            User user = UserData.GetUserByName(userAction.Entity.UserName);
            if (user != null)
            {
                Error += "用户名已被使用. 请选择其他用户名.";
            }
            else
            {
                BlogInfo.Instance().CurrentUser.UserID = 0;
                BlogInfo.Instance().CurrentUser.UserName = user.UserName;
            }
        }

        public void checkpassword()
        {
            if (this.Entity.Password != userAction.repassword)
            {
                Error += "密码不匹配或长度错误. (密码须为6-16 字符)";
            }
            else
            {
                this.Entity.Password = StringSecurity.StringToSHA1Hash(this.Entity.Password);
            }

        }

        public void checkEmail()
        {

        }

        public void checkHomePageUrl()
        {
        }

        public override void checkEntity()
        {
            throw new NotImplementedException();
        }

        public override void checkPowerAgain()
        {
            throw new NotImplementedException();
        }
    }
}
